package com.sndscl.rpc.common.rpc;

import com.alibaba.fastjson.JSONObject;
import com.sndscl.rpc.common.dto.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/23 15:55
 **/
public class RequestEncoder extends MessageToByteEncoder<RpcRequest> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, RpcRequest rpcRequest, ByteBuf byteBuf) throws Exception {
        byte[] bytes = JSONObject.toJSONString(rpcRequest).getBytes(CharsetUtil.UTF_8);
        byteBuf.writeBytes(bytes);
    }
}
